/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package helper;

import javax.microedition.io.Connector;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

/**
 *
 * @author HP
 */
public class SMSSender implements Runnable {

    public SMSSender(String phoneNumber, String message){
        this.phoneNumber = phoneNumber;
        this.message = message;
    }

    public void sendSMS(String phoneNumber, String message){
        MessageConnection conn = null;
        String address = "sms://"+phoneNumber;

        try {
            conn = (MessageConnection) Connector.open(address);
            TextMessage msg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
            msg.setPayloadText(message);

            conn.send(msg);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        if (conn != null){
            try {
                conn.close();
            } catch (Exception ex) {
                System.out.println("Closing connection caught: ");
                ex.printStackTrace();
            }
        }
    }

    public void run() {
        sendSMS(this.phoneNumber, this.message);
    }

    private String phoneNumber;
    private String message;
}
